/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.unl.bsm.CoolingLoad.Email;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;


import edu.unl.bsm.CoolingLoad.Dao.GREEDODaoImpl;
import edu.unl.bsm.CoolingLoad.Dao.CoolingLoadDBDaoImpl;
import edu.unl.bsm.CoolingLoad.Util.Utility;

/**
 *
 * @author zhongyin
 */
public class EmcsMailer
{
	/**
	 * Send a single email.
	 */
	static public void sendEmail(String aFromEmailAddr, List<String> recipientList,String aSubject, String aBody, String mailHostString, String mailUserString)
	{
		sendEmailWithMimeType(aFromEmailAddr, recipientList, aSubject, aBody, mailHostString, mailUserString, "text/plain");
	}
	
	static public void sendHtmlEmail(
		String aFromEmailAddr, ArrayList<String> recipientList,
		String aSubject, String aBody, String mailHostString, String mailUserString)
	{
		sendEmailWithMimeType(aFromEmailAddr, recipientList, aSubject, aBody, mailHostString, mailUserString, "text/html");
	}

	static private void sendEmailWithMimeType(String aFromEmailAddr, List<String> recipientList,String aSubject, String aBody, String mailHostString, String mailUserString, String mimeType)
	{
		//Here, no Authenticator argument is used (it is null).
		//Authenticators are used to prompt the user for user
		//name and password.
		Properties props = new Properties();
		props.setProperty("mail.transport.protocol", "smtp");
		props.setProperty("mail.smtp.host", mailHostString);
//		props.setProperty("mail.user", mailUserString);
		props.setProperty("mail.password", "");

		Session session = Session.getDefaultInstance(props, null);
		MimeMessage message = new MimeMessage(session);
		try
		{
			InternetAddress addressToList[] = new InternetAddress[recipientList.size()];
			for (int i = 0; i < addressToList.length; i++)
			{
				addressToList[i] = new InternetAddress(recipientList.get(i));
			}

			//the "from" address may be set in code, or set in the
			//config file under "mail.from" ; here, the latter style is used
			message.setFrom( new InternetAddress(aFromEmailAddr) );
			message.addRecipients(
				Message.RecipientType.TO, addressToList);
			message.setSubject(aSubject);
			message.setContent(aBody, mimeType);
			Transport.send(message);
		}
		catch (MessagingException ex)
		{
			System.err.println("Cannot send email. " + ex);
		}
	}
	
	public static void main(String[] args){
		
		
		GREEDODaoImpl gdi = GREEDODaoImpl.getInstance();
		CoolingLoadDBDaoImpl ld = CoolingLoadDBDaoImpl.getInstance();
		List<Object[]> clValue = ld.getPredictedValue();
		Collections.reverse(clValue);
		
		List<Double> rmse = ld.getRobustMeanSquaredError();
		Collections.reverse(rmse);
//		Utility.printArrayList(clValue);
//		System.out.println(rmse);
		
		StringBuilder mailContent = new StringBuilder();
		
		String mailTitle = String.format("%15s    | %s | %s | %s |\n", "Target Hour", "Lower Confident Interval", "Cooling Load Value",  "Upper Confident Interval");
		mailContent.append(mailTitle);
//		System.out.println(mailContent);
		int index = 0;
		for(Object[] el: clValue){
			String report = new String();
			if(index < 7){
				report = String.format("%s| %15.2f          | %12.2f       | %15.2f          |\n", el[0].toString().substring(0,19), ((double)el[1]- 2 * rmse.get(index)) <=0 ? 0 :  (double)el[1]- 2 * rmse.get(index), (double)el[1],
						(double)el[1] + 2 * rmse.get(index));
			}else if(index >= 7 && index < 24){
				report = String.format("%s| %15.2f          | %12.2f       | %15.2f          |\n", el[0].toString().substring(0,19), ((double)el[1]- 2 * rmse.get(6)) <=0 ? 0 : (double)el[1]- 2 * rmse.get(6), (double)el[1],
						(double)el[1] + 2 * rmse.get(6));
			}else{
				report = String.format("%s| %15s          | %12.2f       | %15s          |\n", el[0].toString().substring(0,19),"N/A", (double)el[1], "N/A");
			}
			
			index++;
			mailContent.append(report);
		}
		
		System.out.println(mailContent);
		EmcsMailer.sendEmail("emcs@unl.edu",
				 gdi.getEmailRecipientsTest(), "Cooling Load test", mailContent.toString(), 
				"emcs-ws-app1.unl.edu", "");
		
	}
}
